#ifndef CLIENTMANAGER_H
#define CLIENTMANAGER_H

#include <QObject>
#include <QHash>
#include <QMutex>
#include "clientconnection.h"

class ClientManager : public QObject {
    Q_OBJECT
public:
    explicit ClientManager(QObject *parent = nullptr);
    ~ClientManager();

    void addClient(qintptr descriptor);
    void removeClient(qintptr descriptor);
    void broadcastMessage(const QString &message);
    void sendToClient(qintptr descriptor, const QString &message);
    int clientCount() const;
    QList<qintptr> clientList() const;

signals:
    void clientConnected(qintptr descriptor, const QString &info);
    void clientDisconnected(qintptr descriptor);
    void messageReceived(qintptr descriptor, const QString &message);
    void errorOccurred(qintptr descriptor, const QString &error);

private slots:
    void onClientDisconnected(qintptr descriptor);
    void onMessageReceived(qintptr descriptor, const QString &message);
    void onErrorOccurred(qintptr descriptor, const QString &error);

private:
    QHash<qintptr, ClientConnection*> m_clients;
    mutable QMutex m_mutex;
};

#endif // CLIENTMANAGER_H